package com.lbt.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lbt.commonuntils.R;
import com.lbt.eduservice.entity.EduCourse;
import com.lbt.eduservice.entity.EduCourseDescription;
import com.lbt.eduservice.entity.EduTeacher;
import com.lbt.eduservice.entity.vo.CourseInfoVo;
import com.lbt.eduservice.entity.vo.CoursePublishVo;
import com.lbt.eduservice.entity.vo.CourseQuery;
import com.lbt.eduservice.service.EduCourseDescriptionService;
import com.lbt.eduservice.service.EduCourseService;
import com.lbt.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-02
 */
@RestController
//@CrossOrigin
@Api(description = "课程管理")
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    EduCourseService courseService;

    //3、分页查询
    /*分页查询，条件查询
     * */
    //TODO
    @ApiOperation(value = "课程条件分页查询课程")
    @PostMapping("/pageCourse/{current}/{limit}")
    public R selectByCourse(@PathVariable("current") Integer cruent,
                            @PathVariable("limit") Integer limit,
                            @RequestBody CourseQuery courseQuery) {
        Page<EduCourse> pageCourse = new Page<>(cruent, limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        String status = courseQuery.getStatus();
        String title = courseQuery.getTitle();
        if (!StringUtils.isEmpty(status)) {
            wrapper.like("status", courseQuery.getStatus());
        }
        if (!StringUtils.isEmpty(title)) {
            wrapper.ge("title", title);
        }
        courseService.page(pageCourse, wrapper);
        //注意他是将结果直接存进 pageTeacher
        System.out.println("条件分页查询执行了");
        return R.ok().data("total", pageCourse.getTotal()).data("courseInfo", pageCourse.getRecords());
    }

//    @ApiOperation("课程最终发布")
//    @PostMapping("/publishCourse/{id}")
//    public R publishCourse(@PathVariable String id) {
//        EduCourse eduCourse = new EduCourse();
//        eduCourse.setStatus("Normal");
//        eduCourse.setId(id);
//        return R.ok();
//    }

    @ApiOperation(value = "添加课程")
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String id = courseService.save(courseInfoVo);
        return R.ok().data("courseId", id);
    }

    //这个是自己写的还没有用过
    @ApiOperation(value = "获取所有课程")
    @GetMapping
    public R getAllCourse() {
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list", list);
    }

    @ApiOperation(value = "根据课程ID 查询课程基本信息")
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        if (courseId == null) {
            throw new GuliException(20001, "课程Id没有传值");
        }
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    @ApiOperation(value = "修改课程信息")
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfoVo(courseInfoVo);
        return R.ok();
    }

  //  @Cacheable(value = "course")
    @ApiOperation(value = "根据课程ID查询课程确认信息")
    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.getPublishCourseInfo(id);
        return R.ok().data("publishCourse", coursePublishVo);
    }
    //课程最终发布
    //修改课程状态publishCourse
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//设置课程发布状态
        boolean b = courseService.updateById(eduCourse);
        System.out.println("设施是否成功---------------"+b+"---------------设置课程状态");
        EduCourse eduCourse1 = courseService.getById(id);
        System.out.println("课程信息:"+eduCourse1.toString());
        return R.ok();
    }
    @ApiOperation(value = "删除课程")
    @DeleteMapping("/{courseId}")
    public R deleteCourse(@PathVariable("courseId")String courseId){
        courseService.removeCourse(courseId);
        return R.ok();
    }

}
